﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Manage_ClassInfo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Write("<script>alert('请先登录!');window.location.href='../Login.aspx'</script>");
        }
        else
        {
            Bind();
         
  
        }
    }
    public void Bind()
    {
        Bind b = new Bind();
        //GridView1.DataSource = b.ClassInfo();
        //GridView1.DataBind();
        DataView dv = b.ClassInfo().Tables[0].DefaultView;
        PagedDataSource pds = new PagedDataSource();
        AspNetPager2.RecordCount = dv.Count;
        pds.DataSource = dv;
        pds.AllowPaging = true;
        pds.CurrentPageIndex = AspNetPager2.CurrentPageIndex - 1;
        pds.PageSize = AspNetPager2.PageSize;
        GridView1.DataSource = pds;
        GridView1.DataBind();
        this.Label2.Text="("+"本校现共有"+pds.DataSourceCount.ToString()+"个班级"+")";
    }
    protected void cmdAdd_Click(object sender, EventArgs e)
    {
       // if(Convert.ToInt32(Session["Purview"]) >= 5)
       // {
            Response.Write("<script>window.open('AddClass.aspx','','width=350,height=250,top=250,left=280')</script>");
            Response.Write("<script>location='javascript:history.go(-1)'</script>");
       // }
       // else
       // {
         //   Response.Write("<script>alert('对不起,你不够权限！！')</script>");
       // }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        string Classid = GridView1.DataKeys[e.NewEditIndex].Value.ToString();

       // if (Convert.ToInt32(Session["Purview"]) >= 5)
        //{
            Response.Write("<script>window.open('UpdateClass.aspx?Classid="+Classid+"','','width=350,height=250,top=250,left=280')</script>");
            Response.Write("<script>location='javascript:history.go(-1)'</script>");
      //  }
//else
      //  {
       //     Response.Write("<script>alert('对不起,你不够权限！！')</script>");
     //   }
    }
    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        Bind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string Check=DropDownList1.SelectedItem.Value;
        string Info=TextBox1.Text.Trim();
        DataTable dt=new Sqlselete().Select(Check,Info);
        if(dt.Rows.Count==0)
        {
            Response.Write("<script>alert('对不起,没有符合条件的内容！')</script>");
            Bind();
        }
        else
        {
            GridView1.DataSource = dt;
            GridView1.DataBind();
         }   
    }

   
    protected void GridView1_RowDeleting1(object sender, GridViewDeleteEventArgs e)
    {

      //  if (Convert.ToInt32(Session["Purview"]) >= 5)
       // {
            try
            {
                string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
                SqlDelete sd = new SqlDelete();
                sd.DeleteClassInfo(id);
                SqlInsert si = new SqlInsert();
                string WorkInfo = "删除" + id + "班级";
                si.InsertWorkLog(Session["UserName"].ToString(), DateTime.Now.ToString(), WorkInfo);
                Bind();
            }
            catch 
            {
                Response.Write("<script>alert('对不起,存在和此关联的信息,不能删除')</script>");
            }
     //   }
      //  else
      //  {
            Response.Write("<script>alert('对不起,你不够权限！！')</script>");
      //  }
    }
    protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[7].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除班级：\"" + e.Row.Cells[0].Text + "\"吗?')");
            }
        }
       
    }
}
